'''
旋转页面

 pip3  install  -i  https://pypi.doubanio.com/simple/ pypdf2
'''

from PyPDF2 import PdfFileReader, PdfFileWriter


def rotate_pages(pdfIn):
    pdf_writer = PdfFileWriter()
    pdf_reader = PdfFileReader(pdfIn)
    # 将页面向右旋转90度
    # PyPDF2 软件包仅允许您以 90 度为增量旋转页面。否则，您将收到一个 AssertionError。
    page_1 = pdf_reader.getPage(0).rotateClockwise(90)
    # 添加到新的pdf中
    pdf_writer.addPage(page_1)
    # 将页面向左旋转90度
    # 一页可以重复旋转，该页最终的旋转状态以该页最后的操作为准
    page_2 = pdf_reader.getPage(1).rotateCounterClockwise(180)
    pdf_writer.addPage(page_2)
    # 添加一页正常的pdf
    # pdf_writer.addPage(pdf_reader.getPage(2))

    with open('岳阳楼记_rotate.pdf', 'wb') as fh:
        pdf_writer.write(fh)

if __name__ == '__main__':
    pdfpath = '岳阳楼记.pdf'
    with open(pdfpath, 'rb') as f:
        rotate_pages(f)